package com.yc.jpyy.Rreportfordataacquisition.controller;


import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.Rreportfordataacquisition.service.ReportForDataAcquisitionService;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.utils.Tools;


@Controller
@RequestMapping(value = "/Report")
@Scope("prototype")
public class ReportForDataAcquisitionController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(ReportForDataAcquisitionController.class);

	@Autowired
	private ReportForDataAcquisitionService reportForDataAcquisitionService;
	
	/**
	 * 根据车辆类型分类汇总车辆运行统计信息
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getCarBillingAmount", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String getCarBillingAmount(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			String checkParameter= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(checkParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = reportForDataAcquisitionService.getCarBillingAmount(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("getCarBillingAmount" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
	/**
	 * 根据时段分类汇总跑的单子统计信息
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getPeriodSummary", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String getPeriodSummary(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			String checkParameter= request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(checkParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = reportForDataAcquisitionService.getPeriodSummary(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("getPeriodSummary" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
		
	}
	
}
